<?php
require_once('functions.php');

function getInfos()
{
	$conn = getConnection();
	return $conn->query('SELECT * FROM Employee_Info');
}

function getInfo($id)
{ 
		$conn = getConnection();
        $results = $conn->query("SELECT * FROM Employee_Info WHERE user_ID=$id");
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}

function addInfo($row)
{
        $conn = getConnection();
        $row2 = escapeRow($row, $conn);
        
        $results = $conn->query($sql);
        $error = $conn->error;
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}

function noInfo()
{
	$conn = getConnection();
	$sql = "SELECT u.user_ID, firstName, secondName FROM User u LEFT JOIN Employee_Info ei ON u.user_ID = ei.user_ID WHERE ei.user_ID IS NULL AND u.status_ID ='2'";
	$results = $conn->query($sql);
    $conn->close();
       
    return $results;
}

function blankInfo($id)
{
	return array('user_ID' => $id, 'payRate' => null, 'status' => null, 'department' => null);
} 

function deleteInfo($id)
{
		$conn = getConnection();
		$conn->query("DELETE FROM Employee_Info WHERE user_ID = '$id'");
		$conn->close();
		header('Location: index.php');
}
		
function saveInfo($row)
{
        $conn = getConnection();
        $row2 = escapeRow($row, $conn);
        
        if (getInfo($row['user_ID']))
        	$sql =  "UPDATE Employee_Info SET payRate='$row2[payRate]', status='$row2[status]', department='$row2[department]' WHERE user_ID= '$row2[user_ID]'";
        else
        	$sql =  "INSERT INTO Employee_Info VALUES ('$row2[user_ID]', '$row2[payRate]', '$row2[status]', '$row2[department]')";
        	
        $results = $conn->query($sql);
        $error = $conn->error;
        
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}

